1 /*
2 * Copyright (c) 2001, 2002 The XDoclet team
3 * All rights reserved.
4 */
5 package xdoclet;
6
7 import java.util.regex.Matcher;
8 import java.util.regex.Pattern;
9
10 /***
11 * Uses JDK 1.4 java.util.regex to perform package substitutions.
12 * For example:
13 *
14 * <pre>
15 * foo.bar.ejb.MyBean
16 * from: ejb
17 * to: ejb.interfaces
18 * </pre>
19 *
20 * @author <a href="mailto:aslak.hellesoy at netcom.no">Aslak Hellesøy</a>
21 * @version $Revision: 1.6 $
22 */
23 public class PackageSubstitution {
24 private Pattern _pattern;
25 private String _to = "";
26
27 /***
28 * Sets the part of the package name to replace from. This can be a regexp.
29 *
30 * @param from the part of the package name to replace from
31 */
32 public void setFrom(String from) {
33 if (from == null) {
34 throw new IllegalArgumentException("from cannot be null");
35 }
36
37 // From the SUN Code Conventions for the JavaTM Programming Language:
38 // The first component of a unique package name is always written in all-lowercase
39 // ASCII letters and should be one of the top-level domain names, currently com, edu,
40 // gov, mil, net, org, or one of the English two-letter codes identifying countries as
41 // specified in ISO Standard 3166, 1981.
42 _pattern = Pattern.compile(from, Pattern.CASE_INSENSITIVE);
43 }
44
45 /***
46 * The String to replace with.
47 *
48 * @param to the String to replace with
49 */
50 public void setTo(String to) {
51 _to = to;
52 }
53
54 /***
55 * Gets the substituted package name.
56 *
57 * @param packageName the package name to substitute.
58 * @return substituted package name or null if from was not matched.
59 */
60 public String getSubstitutedPackageName(String packageName) {
61 <b>if (packageName == null) {
62 throw new IllegalArgumentException("packageName cannot be null");
63 }
64
65 if (_pattern == null) {
66 throw new IllegalStateException("pattern is not compiled (null), set the from first");
67 }
68
69 Matcher matcher = _pattern.matcher(packageName);
70
71 assert matcher != null : "matcher is null";
72
73 String substituted = matcher.replaceAll(_to).toLowerCase();
74
75 return packageName.equals(substituted) ? null : substituted;
76 }
77 }
This page was automatically generated by Maven